package DDSClient;

import DDSInterfaces.IDownloadServer;
import DDSInterfaces.ServerBandwidthBean;
import java.rmi.registry.*;

import jade.lang.acl.*;
import jade.content.*;
import jade.content.onto.basic.*;
import jade.content.lang.sl.*;
import jade.core.*;
import jade.domain.mobility.*;
import jade.domain.JADEAgentManagement.*;
import java.util.ArrayList;
import jade.core.Agent;
import jade.wrapper.StaleProxyException;

/**
 *
 * @author Rasmus Bo Sorensen
 * @author Bo Visfeldt
 */
public class Client {
    Registry registry;
    jade.wrapper.AgentContainer signPostAgentContainer;

    public static void main(String[] args) throws Exception{
        if(args.length<2){
            System.exit(-1);
        }
        System.out.println(args[0]);
        Client client = new Client(args[0], args[1]);
    }

    public Client(String aTitle, String signPostAgentName) throws Exception{
        setupJadeClientContainer();
        createClientSignPostAgent(signPostAgentName);

        // The download server does not change, therefore we have hardcoded the values
        int port = 2221;
        boolean completed = false;
        String host = "localhost";
        String name = "DownloadServer";

        System.out.println("Contacting '"+name+"' @ "+host+":"+port+" to search for the title '"+aTitle+"'");
        while(!completed){ // The while loop should only execute once, unless the is a timeout in form of an exception form the RMI call.
            try {

                registry = LocateRegistry.getRegistry("localhost", port);
                IDownloadServer ds = (IDownloadServer)(registry.lookup(name));
                ds.RequestServer(aTitle,signPostAgentName);
            }
            catch (Exception e) {
                e.printStackTrace(); //not completed for some reason...
                System.out.println("Received exception, retransmitting request...");
                completed = false;
            }
        }
    }

        //Container so the DownloadServers agent can migrage into this and report back
        private void setupJadeClientContainer() throws Exception{
            try{
                System.out.println("Fetching JADE runtime enviroment...");
                jade.core.Runtime runtime = jade.core.Runtime.instance();
                System.out.println("Creating JADE client container...");
                //ProfileImpl profileToAvoidCrappyLinuxFailure = new ProfileImpl("127.0.0.1",1099, "ClientContainer",false);
                ProfileImpl profileToAvoidCrappyLinuxFailure = new ProfileImpl();
                //some implementations of Ubuntu has localhost as 127.0.1.1 which messes with Java's localhost
                signPostAgentContainer = runtime.createAgentContainer(profileToAvoidCrappyLinuxFailure);

            }
            catch(Exception e){
                e.printStackTrace();
                throw e;
            }
        }

        //The Downloadservers agent cannot fint the container on the client and migrate to it if there
        //is no agent present here. Therefore a signpost agent has been created. It will never be used,
        //and is only made so the traveling agent can find us (yep, this sucks but JADE can only find
        //a location from an agent name (WhereIsAgentAction) or retrieve ALL locations (QueryPlatformLocationsAction))
        private void createClientSignPostAgent(String clientSignPostName) throws Exception{
            System.out.println("Creating SignPostAgent");
            Object[] args = new Object[0];
            jade.wrapper.AgentController agentController = signPostAgentContainer.createNewAgent(clientSignPostName,"DDSClient.SignPostAgent",args);
            System.out.println("SignPostAgent created...");
            agentController.start();
        }
}
